Part Number Hot Search : 
PN2933 HV264 AS170 61LV1 OP27EP 37N06 BBY5702 C8225
Product Description
Full Text Search
 

To Download AN2822 Datasheet File

  If you can't view the Datasheet, Please click here to try to view without PDF Reader .  
 
 


  Datasheet File OCR Text:
  august 2011 doc id 14983 rev 2 1/19 AN2822 application note stm8s and stm8a high speed in ternal oscillator calibration introduction this application note describes how to take ad vantage of the 16 mhz internal rc oscillator trimming feature on the stm8s and stm8a microcontroller to improve the frequency accuracy of the system compensating voltage and temperature frequency drift. the calibration technique, using the mains frequency as a timebase, is presented with an example of both hardware and software solutions. the example source code can be downloaded from the www.st.com . to benefit fully from the inform ation in this application note, the user should be familiar with the stm8s and stm8a microcontroller architecture and with the basics of c language. for further information on the stm8s and stm8a family features, pinout, electrical characteristics, mechanical data and ordering information, please refer to the stm8s and stm8a datasheets. this document and its associated firmware, are written to accompany the stm8s and stm8a firmware library. please contact stmicr oelectronics for information on the stm8s and stm8a firmware library. www.st.com
contents AN2822 2/19 doc id 14983 rev 2 contents 1 high speed internal oscillator tr imming . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.1 introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.2 calibration principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1.3 calibration routine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.4 measurement of hsi pulses per calibration period . . . . . . . . . . . . . . . . . . 9 1.5 timing of calibration period measurement . . . . . . . . . . . . . . . . . . . . . . . . 11 1.6 uncertainty of calibrated hsi frequency . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2 power supply and calibrati on signal conditioning circui t . . . . . . . . . . 15 3 conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 4 revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
AN2822 list of tables doc id 14983 rev 2 3/19 list of tables table 1. clk_hsitrimr values and two?s-complement binary representation . . . . . . . . . . . . . . . . 6 table 2. value of used components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 table 3. document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
list of figures AN2822 4/19 doc id 14983 rev 2 list of figures figure 1. stm8s and stm8a hsi trimming principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 figure 2. hsi calibration set-up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 figure 3. hsi calibration flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 figure 4. measurement of hsi pulses per calibration period flowchart . . . . . . . . . . . . . . . . . . . . . . . 10 figure 5. timing diagram of calibration period measurement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 figure 6. example of calibration signal conditioning circuit and 5 v power supply . . . . . . . . . . . . . . 15
AN2822 high speed internal oscillator trimming doc id 14983 rev 2 5/19 1 high speed internal oscillator trimming 1.1 introduction the stm8s and stm8a 8-bit microcontrollers can use a high speed internal (hsi) rc oscillator as a system clock source. th is oscillator has a nominal frequency, f hsi nominal , of 16 mhz which is factory calibrated at an ambient temperature of t a = 25 c and a supply voltage of v cc = 5 v. this accuracy is specified in the datasheet for each device. the hsi frequency is calibrated to give reas onable accuracy for the vast majority of applications. however, the outpu t frequency of the rc oscillator is affected by temperature and/or voltage drift, while, total accuracy of the hsi frequency is reduced a percentage given in the datasheet. to compensate for such influences in the application, the stm8s and stm8a hsi oscillator output frequency can be trimmed by the user runtime calibration routine to improve frequency accuracy. this is especially important for communication peripherals. figure 1. stm8s and stm8a hsi trimming principle initial calibration data, init_trim, are stored in the protected part of the internal memory and loaded onto the internal calibration register after reset. the frequency of the hsi rc oscillator can be fine-tuned to achiev e better accuracy with wider temperature and supply voltage ranges. the lowest three bits of the clk_hsitrimr register are reserved for this purpose. the number stored in these bits is processed as a signed number with the two's-complement representation (see ta b l e 1 ). the trimming number (a) can vary from -4 (100b) to +3 (011b). this number is added to the factory calibration value. note: an increase in this trimming value causes a decrease in hsi frequency. the frequency change per step is approximately 1% (b) . a. some selected stm8s and stm8a devices can be set to 4-bit trimming mode by the hsitrim option byte, see device datasheet for details. rc oscillator decoder clk_hsitrim hsi clock output + init_trim b. if the 4-bit trimming mode is set by the hsitrim opt ion byte, the frequency change between two steps in the trimming value is approx. 0.5%
high speed internal oscillator trimming AN2822 6/19 doc id 14983 rev 2 note: the default value on the clk_hsitrimr register is zero (000b) after reset. 1.2 calibration principle to calibrate the hsi output frequency, a reference timebase is necessary. a simple reference timebase can be used, such as the post-processed signal from the mains power source (see section 2 ). the final accuracy of the hsi frequency is strongly dependent on the accuracy of the reference timebase. better accuracy can be achieved with more accurate timebases such as a signal generator. figure 2. hsi calibration set-up the calibration signal, from the mains power supply, is connected to the tim3_cc1 input pin and is used as a timebase which allows the mi crocontroller to test if the hsi rc oscillator frequency is above or below the nominal frequency. the hsi frequency is sequentially tuned by the microcontroller until the optimum value is reached. table 1. clk_hsitrimr values and two?s-complement binary representation decimal value binary two?s-complement value 3011 2010 1001 0000 -1 111 -2 110 -3 101 -4 100 tim3_cc1 /1, /2, /4 or /8 counter capture f master = f hsi core cco overload interrupt capture interrupt clk_hsitrimr hsi 16 mhz prescaler calibration signal input filter ti1 ti1f ic1ps
AN2822 high speed internal oscillator trimming doc id 14983 rev 2 7/19 the hsi frequency is evaluated from the number of pulses counted by timer tim3 during certain calibration signal periods. for noisy mains, where positive and negative half-periods are not necessarily symmetrical, it is better to measure the whole period and not only the time difference between the rising and falling edge of the calibration signal. it is better to measure the number of hsi pulses for several sequential periods to reduce the uncertainty of the measurement. this is also recommended when using a noisy mains as the calibration signal. the input capture prescaler can be used for this purpose. it allows every 1st, 2nd, 4th, or 8th rising edge of the calibration signal to be selected which is then used to define the time interval for counting hsi pulses. in this way tim3 is able to store information about the number of hsi pulses in the capture register at the beginning and at the end of a measurement. if required (for example, for validation purposes), the frequency of the hsi oscillator can be observed on the configurable clock output (cco) pin. see section 2 for an example of a simple post-processing circuit which adapts a mains signal to a square signal and converts the mains voltage to 5 v power supply voltage. 1.3 calibration routine the calibration routine is written in ansi c. it takes advantage of the stm8s and stm8a firmware library which is available from www.st.com . it can be simplified and optimized if needed. the software compares the number of hsi clock pulses during the certain number l of calibration period, t cal (20 ms), with the ideal value (see equation below). using this equation, the calibration routine software compares the ideal hsi clock pulse value (idealcountedvalue) with th e nominal oscillato r frequency, f hsinominal , during a defined calibration period, t cal (20 ms), for a known number of calibration signal periods (l). note: for example, if the nominal hsi frequency is 16 mhz, the defined calibration period, t cal , is 20 ms (if the mains has a frequency of 50 hz), and the known number of calibration signal periods, l, is 8 then the ideal value captured by the timer is 2 560 000 (0x271000) the calibration algorithm is described in figure 3 . calibration starts with initia lization of tim3. the lowest frequency is set by writing max_trim_val to the clk_hsitrimr register . in this way, the trimming value is decreased in the program loop. the difference between the number of counted hsi pulses and the ideal value is compared with the previous difference, until either the current difference becomes greater than the preceding difference, or, until the minimum value min_trim_val is reached. if the new difference is greater than the pr eceding difference, the trimming value is incremented to the previous value, result ing in the optimum value for calibration. idealcountedvalue f hsi nominal t cal l =
high speed internal oscillator trimming AN2822 8/19 doc id 14983 rev 2 figure 3. hsi calibration flowchart 1. the ?abs? function calc ulates absolute value. the measurement routine results are the sum of hsi pulses during the consequential calibration period. the number of calibration signal periods, l, used for the measurement are given by the constant, max_averaged_period. the more periods used for the measurement, the lower the uncertainty of period length due to the averaging effect. the recommended value range is 4 or 8. prevdiff < actdiff increment clk_hsitrim end of calibration yes no start of calibration clk_hsitrim = max_trim_val initialization of tim3 measure the hsi pulses, calculate abs (actual ? ideal), store it in prevdiff clk_hsitrim min_trim_val yes no decrement clk_hsitrim measure the hsi pulses calculate 1) abs (actual ? ideal) store it in actdiff prevdiff = actdiff
AN2822 high speed internal oscillator trimming doc id 14983 rev 2 9/19 1.4 measurement of hsi pul ses per calibration period the current hsi oscillator frequency, f hsi , is not measured directly. it is evaluated from the number of counted hsi clock pulses, n hsi , during a defined calibration interval, t cal , for a known number of calibration signal periods (l), using the following equation: tim3 timer counts the number of hsi pulses with minimum interaction from the software routine. this helps to reduce measurement uncertainty caused by the software routine reaction times. in addition, the mcu core is not occupied by the software and can continue to execute user code. the measurement of hsi pulses starts with the initialization of tim3 timer. it is configured to count the number of hsi pulses and to handle two interrupt events. the first interrupt occurs when the timer overflows. the second interrupt is an input capture interrupt which occurs every l th rising edge of the calibration period signal. when the first rising edge of the calibration signal period occurs, the value in the tim3_cntr register is stored in the input capture register, tim3_ccr, and the content of this register is saved during the input capture interrupt into the firstcapture variable. the last input capture interrupt occurs when the l th rising edge of the calibration signal is detected and the value in the input capture register tim3_ccr is saved into the lastcapture variable. the number of overflows between the first and last capture events is increased at every overflow interrupt and stored in the overflow variable. when the last input capture event occurs the state measure_compl eted is set and the timer is stopped. then the number of hsi pulses is evaluated using the equation below: figure 4 summarizes the measurement of hsi pulses for a known calibration signal period. f hsi n hsi t cal l ? -------------------- - = n hsi lastcapture 0x10000 overflows firstcapture ? + =
high speed internal oscillator trimming AN2822 10/19 doc id 14983 rev 2 figure 4. measurement of hsi pulses per calibration period flowchart enter ?overflow interrupt? exit interrupt increase the number of overflows measurestate = measure_in_ progress yes no measurestate ? exit interrupt enter ?input capture interrupt? measure_start measure_in_progress disable interrupts stop the counter set measurestate to measure_in_ progress save captured value in firstcapture set measurestate to measure_ completed save captured value in lastcapture initializing of variables
AN2822 high speed internal oscillator trimming doc id 14983 rev 2 11/19 1.5 timing of calibration period measurement this section describes the timing of the hsi oscillator frequency, f hsi measurement. the ?digital filtering technique? is applied on the ti1 input from tim3_cc1 (see figure 2 ) to further reduce period measurement uncertainty. this technique includes an event counter, present on the input capture pin, in which a certain number of ti1 samples with the same level are needed to validate a transition of the reference timebase. the number of such pulses and the sampling frequency can be set by bits ic1f[3:0] in the tim3_ccmr1 register. the sampling frequency can be set as an f master divided by 1 to 32. by default, eight equal consecutive samples with a time resolution of 1/f master , are required for the transition to be valid. this default value can be changed if needed. this digital filtering technique causes a delay between ti1 and the ic1 signal. it does not affect the measurement of hsi pulses per calibration interval (see figure 5 ). figure 5. timing diagram of ca libration period measurement counter register value ovf ti1 ic1ps cc ovl ovl ovl ovl ovl filter delay l = 4 cc ovl
high speed internal oscillator trimming AN2822 12/19 doc id 14983 rev 2 1.6 uncertainty of calibrated hsi frequency there are two sources of uncertainty (c) associated with this calibration method. the first one is caused by the finite resolution of the trimming value. the second is the uncertainty of the hsi frequency measurement used to regulate hsi output frequency using the hsi trimming register. the calibrated hsi oscillator frequency, f hsim , is not measured directly. we can start with the equation describing the estimated hsi frequency as a function of each of the factors that can influence it to evaluate the uncertainty of the calibrated hsi output frequency given by the calibration method (d) as l is a constant we can identify two uncertainty contributors. the first is a number of counted hsi pulses, n hsi , the second is a calibration period, f cal . these input parameters can be assumed as uncorrellated quantities. the uncertainty of n hsi is caused by the nonsynchronous frequency of the calibration signal and hsi oscillator frequency. the first rising edge of the l-series of calibration signal periods can occur anywhere in the interval of one hsi signal period as well as the last rising edge which close the counter gate, resulting in the uncertainty of counted hsi pulses equal to 1. assuming uniform uncertainty distribution, the standard uncertainty of n hsi can be evaluated using the following equation to reduce this uncertainty, the calibration frequency should be well below the hsi frequency (at least 1000 times lower). this is easily ac hieved when the mains frequency is used as a calibration signal. the uncertainty of the calibration signal frequency, f cal , is given by the fluctuation of the mains frequency. normally, the accuracy of the mains frequency, f cal , is better than 1%. however, even better accuracy can be achieved by using a different frequency source, such as a signal generator. to reduce uncertainty from this source to a minimum, the frequency generator which gives the best accuracy should be used instead of the mains frequency. the standard uncertainty of calibration frequency, f cal , is expressed below assuming uniform uncertainty distribution. from the equation describing the estimated hsi frequency, f hsi , we can calculate the sensitivity coefficient associated with each of the input estimates. c. this analysis of the hsi calibrat ion method is based on iso guide to the expression of uncertainty in measurement and ansi/ncsl z540-2-1997. these standards offer a current internat ional consensus of the methods for estimating measurement uncertainty. d. estimates of uncertainty obtained in this way are usually called type b. f hsim n hsi l ------------ - f cal = un hsi () 1 3 ------- = uf cal () f cal f cal 100 3 ---------------------- - =
AN2822 high speed internal oscillator trimming doc id 14983 rev 2 13/19 the combined standard uncertainty of the hsi frequency measurement can be calculated by the equation bellow. when the calibration is finished, the o scillator output frequency will be at maximum 1/2 of the frequency trimming step, f trim , far from the desired frequency. this finite regulation steps causing additional uncertainty which can be expressed (e) by the following equation. the total combined uncertainty of the hsi frequency can be expressed using the equation below. example let?s consider as a calibration signal mains, f cal , with a frequency of 50 hz with accuracy, f cal , in the range of 1%, number of calibration signal periods l = 4 and a desired output frequency 16 mhz. evaluation of the uncertainty of input quantities and sensitivity coefficients are the following. e. assuming a uniform distribution of the hsi output frequency uncertainty. c n hsi n hsi ? ? f hsim f cal l -------- == c f cal f cal ? ? f hsim n hsi l ------------ - == u c 2 f hsim () c i 2 ui () 2 i f cal l -------- ?? ?? 2 1 3 ------- ?? ?? 2 n hsi l ------------ - ?? ?? 2 f cal f cal 100 3 ---------------------- - ?? ?? 2 + == uf trim () f trim 23 -------------- = uf hsi () u 2 f trim () u c 2 f hsim () + = un hsi () 1 3 ------- 1.732 ? = uf cal () f cal f cal 100 3 ---------------------- - 50 100 3 ----------------- 0.286 ? == c n hsi n hsi ? ? f hsim f cal l -------- 12.5 ? ==
high speed internal oscillator trimming AN2822 14/19 doc id 14983 rev 2 the combined standard uncertainty of the hsi frequency measurement is evaluated bellow. comparing the first and second terms we can find that a contribution caused by uncertainty, u(n hsi ), is negligible and can be omitted. this statement is valid if the calibration frequency is well below the hsi frequency. let?s evaluate the uncertainty caused by the finite regulation steps. finally, the total combined uncertainty of the hsi frequency is evaluated bellow. assuming that the required confidence level is 95% using the coverage factor k=2, we can express the extended uncertainty, u, as following. c f cal f cal ? ? f hsim n hsi l ------------ - 320 10 ? 3 468.7 8.376 10 9 ? () ? == u c 2 f hsim () c n hsi 2 u 2 n hsi () c f cal 2 u 2 f cal () + 468.7 8.376 10 9 ? + ? = u c f hsim () 91.52 khz ? () 160 10 3 ? 23 -------------------------- 46.19 khz ? = uf hsi () u 2 f trim () u c 2 f hsim () + 102.5 khz ? = ukuf hsi () ? 2 102.5 khz 205 khz ? ? ? =
AN2822 power supply and calibrat ion signal conditioning circuit doc id 14983 rev 2 15/19 2 power supply and calibration signal conditioning circuit figure 6 shows the calibration signal conditioning circuit which also provides approximately 5 v of dc power supply to the microcontroller. where power supply from the mains is not needed, only a single resistor between power line and tim3_cc1 input is adequate to protect this pin from over current in normal operating conditions. the calibration signal conditioning circuit contains a few passive components which convert either the 230 v / 50 hz of a european mains or the 110 v / 60 hz of a us mains to a 5 v dc power supply. an application with more current consumption should use a power converter (see an1357, vipower: low cost power supplies using viper12a in non isolated applications). caution: the calibration signal conditioning and power supply circuits cannot be used if there are big current variations. figure 6. example of calibration signal conditioning circuit and 5 v power supply if using a noisy mains, an input power line filter is recommended (see an2326, calibrating the rc oscillator of the st7ult ralite mcu using the mains). r2 c3 c1 + r1 r4 v dd tim3_cc1 stm8 n l ac mains ~ d1 d2 c2 v ss r3
power supply and calibration signal conditioning circuit AN2822 16/19 doc id 14983 rev 2 table 2. value of used components component 230 v / 50 hz value 110 v / 60 hz value r1 220 / 0.5 w 110 / 0.5 w r2 220 / 0.5 w 110 / 0.5 w r3 1 m 1 m r4 5.6 k 5.6 k d1 bzx85c5.6 bzx85c5.6 d2 1n4148 1n4148 c1 470 nf / ~275 v ac 330 nf / ~275 v ac c2 100 nf 100 nf c3 470 f / 16 v 470 f / 16 v
AN2822 conclusion doc id 14983 rev 2 17/19 3 conclusion the calibration method described in this application note, ensures that the high speed internal clock of the stm8s and stm8a microcontrollers operates at its intended frequency (16 mhz) in the target application, independently of the temperature and power supply voltage drift. the proposed solution us es the 16-bit timer tim3 for meas uring the internal rc oscillator frequency and external calibration signal which can be easily adapted from the mains. an implementation example with power supply and calibration signal conditioning circuit is proposed. the example source code written in ansi c and using the stm8s and stm8a firmware library can be download from www.st.com .
revision history AN2822 18/19 doc id 14983 rev 2 4 revision history table 3. document revision history date revision changes 20-jan-2009 1 initial release 31-aug-2011 2 updated to refer to stm8s and stm8a products.
AN2822 doc id 14983 rev 2 19/19 please read carefully: information in this document is provided solely in connection with st products. stmicroelectronics nv and its subsidiaries (?st ?) reserve the right to make changes, corrections, modifications or improvements, to this document, and the products and services described he rein at any time, without notice. all st products are sold pursuant to st?s terms and conditions of sale. purchasers are solely responsible for the choice, selection and use of the st products and services described herein, and st as sumes no liability whatsoever relating to the choice, selection or use of the st products and services described herein. no license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted under this document. i f any part of this document refers to any third party products or services it shall not be deemed a license grant by st for the use of such third party products or services, or any intellectual property contained therein or considered as a warranty covering the use in any manner whatsoev er of such third party products or services or any intellectual property contained therein. unless otherwise set forth in st?s terms and conditions of sale st disclaims any express or implied warranty with respect to the use and/or sale of st products including without limitation implied warranties of merchantability, fitness for a parti cular purpose (and their equivalents under the laws of any jurisdiction), or infringement of any patent, copyright or other intellectual property right. unless expressly approved in writing by two authorized st representatives, st products are not recommended, authorized or warranted for use in milita ry, air craft, space, life saving, or life sustaining applications, nor in products or systems where failure or malfunction may result in personal injury, death, or severe property or environmental damage. st products which are not specified as "automotive grade" may only be used in automotive applications at user?s own risk. resale of st products with provisions different from the statements and/or technical features set forth in this document shall immediately void any warranty granted by st for the st product or service described herein and shall not create or extend in any manner whatsoev er, any liability of st. st and the st logo are trademarks or registered trademarks of st in various countries. information in this document supersedes and replaces all information previously supplied. the st logo is a registered trademark of stmicroelectronics. all other names are the property of their respective owners. ? 2011 stmicroelectronics - all rights reserved stmicroelectronics group of companies australia - belgium - brazil - canada - china - czech republic - finland - france - germany - hong kong - india - israel - ital y - japan - malaysia - malta - morocco - philippines - singapore - spain - sweden - switzerland - united kingdom - united states of america www.st.com


▲Up To Search▲   

 
Price & Availability of AN2822

All Rights Reserved © IC-ON-LINE 2003 - 2022  

[Add Bookmark] [Contact Us] [Link exchange] [Privacy policy]
Mirror Sites :  [www.datasheet.hk]   [www.maxim4u.com]  [www.ic-on-line.cn] [www.ic-on-line.com] [www.ic-on-line.net] [www.alldatasheet.com.cn] [www.gdcy.com]  [www.gdcy.net]


 . . . . .
  We use cookies to deliver the best possible web experience and assist with our advertising efforts. By continuing to use this site, you consent to the use of cookies. For more information on cookies, please take a look at our Privacy Policy. X